package com.goods.service.impl;

import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.service.SupplierService;
import com.google.common.base.Suppliers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    SupplierMapper supplierMapper;

    @Autowired
    ProductMapper productMapper;

    @Override
    public List<SupplierVO> findProductList(String name,String address,String contact) {
        List<Supplier> suppliers = null;
        if (StringUtils.isEmpty(name)&&StringUtils.isEmpty(address)&&StringUtils.isEmpty(contact)) {
            suppliers = supplierMapper.selectAll();
        }else {
            suppliers = supplierMapper.selectByName(name,address,contact);
        }
        List<SupplierVO> supplierVOList = suppliers.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            supplierVO.setId(supplier.getId());
            supplierVO.setName(supplier.getName());
            supplierVO.setAddress(supplier.getAddress());
            supplierVO.setEmail(supplier.getEmail());
            supplierVO.setCreateTime(supplier.getCreateTime());
            supplierVO.setModifiedTime(new Date());
            supplierVO.setPhone(supplier.getPhone());
            supplierVO.setSort(supplier.getSort());
            supplierVO.setContact(supplier.getContact());
            return supplierVO;
        }).collect(Collectors.toList());

        return supplierVOList;
    }

    @Override
    public int add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        supplier.setName(supplierVO.getName());
        supplier.setAddress(supplierVO.getAddress());
        supplier.setEmail(supplierVO.getEmail());
        supplier.setContact(supplierVO.getContact());
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplier.setPhone(supplierVO.getPhone());
        supplier.setSort(supplierVO.getSort());
        int i = supplierMapper.insert(supplier);
        return i;
    }

    @Override
    public Supplier edit(Long id) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        System.out.println(new Date());
        Supplier supplier1 = supplierMapper.selectOne(supplier);
        return supplier1;
    }

    @Override
    public int update(Long id, SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        supplier.setSort(supplierVO.getSort());
        supplier.setContact(supplierVO.getContact());
        supplier.setEmail(supplierVO.getEmail());
        supplier.setName(supplierVO.getName());
        /*supplier.setCreateTime(supplierVO.getCreateTime());
        supplier.setModifiedTime(new Date());*/
        supplier.setPhone(supplierVO.getPhone());
        supplier.setAddress(supplierVO.getAddress());
        int i = supplierMapper.updateByPrimaryKey(supplier);
        return i;
    }

    @Override
    public int delete(Long id) {
        Supplier supplier = new Supplier();
        supplier.setId(id);
        return supplierMapper.delete(supplier);
    }

    @Override
    public List<Supplier> findAll() {
        return supplierMapper.selectAll();
    }
}
